from seaman.core import JsonUtil

from fish.common import FishParams
import math
from sympy import symbols, integrate
from scipy.integrate import quad
from scipy.optimize import bisect


# 计算掉落电缆的长度
def get_drop_length(params:FishParams) -> float:
    L_c: float = params.L_d - params.L_t
    return L_c


# 计算电缆沿井深分布的轴向压力
# 1.求出电缆在液体中的重量
# 2.电缆沿井深的轴向压力F_Z
# 1.求出电缆在液体中的重量
def get_cable_weight(params: FishParams) -> float:
    q_l: float = params.q_s - math.pi * 0.25 * math.pow(params.d, 2) * params.g * params.rou_l
    return q_l


# 2.电缆沿井深的轴向压力F_Z
def get_axial_pressure(params: FishParams) -> float:

    F_z: float = get_cable_weight(params) * params.z
    return F_z


class SpiralResult:
    # P_m: float
    F_hel: float
    Z_hel: float
    L_hel: float


# 确定螺旋段长度
def get_spiral_length(params: FishParams) -> SpiralResult:
    result = SpiralResult()
    # ξ_c≥4.12(q_l/EI)^(1⁄3),电缆为螺旋变形
    ql: float = get_cable_weight(params)
    L_c: float = get_drop_length(params)
    # I为电缆截面惯性距
    I: float = math.pi * math.pow(params.d, 4) / 64
    # factor_a: float = 9 * math.pow(params.m, 2) * math.pow(math.pi, 2) * params.E * I / ql
    # factor_b: float = 9 * math.pow(params.m - 1, 2) * math.pow(math.pi, 2) * params.E * I / ql
    # # 1.第m个螺旋个数的螺距p_m
    # P_m: float = math.pow(factor_a, 1/3) - math.pow(factor_b, 1/3)
    # result.P_m = P_m
    # 2.螺旋段临界压力
    epsilon_hel: float = 4.12 * math.pow(ql / (params.E * I), 1/3)
    F_hel: float = epsilon_hel * ql * math.pow((params.E * I) / ql, 1/3)
    result.F_hel = F_hel
    # 3.对应电缆位置
    Z_hel: float = F_hel / ql
    result.Z_hel = Z_hel
    # 4.螺旋段长度
    L_hel: float = L_c - Z_hel
    result.L_hel = L_hel
    return result


# 确定螺旋段区间
def get_spiral_interval(params:FishParams) -> dict[str, float]:
    spiral: SpiralResult = get_spiral_length(params)
    Z_hel: float = spiral.Z_hel
    L_c: float = get_drop_length(params)
    spiral_interval = {}
    spiral_interval['low'] = Z_hel
    spiral_interval['high'] = L_c
    return spiral_interval


class SinResult:
    epsilon_sin: float
    L_sin: float
    F_sin: float
    Z_sin: float
    li: float


# 确定正弦段长度
def get_sin_length(params: FishParams) -> SinResult:
    result = SinResult()
    ql: float = get_cable_weight(params)
    # I为电缆截面惯性距
    I: float = math.pi * math.pow(params.d, 4) / 64
    F_z: float = get_axial_pressure(params)
    # 1.根据无量纲轴向压力与无量纲长度的函数表达式
    epsilon_c: float = math.pow(ql/(params.E * I), 1/3) * F_z / ql
    ri: float = 152.512 * math.pow(math.e, - epsilon_c / 0.437) + 3.251 * math.pow(math.e, - epsilon_c / 1.806) \
               + 1.295 * math.pow(math.e, - epsilon_c / 9.689) + 0.544
    # 2.实际临界长度
    li: float = ri * math.pow(ql / (params.E * I), 1/3)
    result.li = li
    # 3.正弦段临界压力
    epsilon_sin: float = 2.648 * math.pow(ql / (params.E * I), 1/3)
    result.epsilon_sin = epsilon_sin
    F_sin: float = epsilon_sin * ql * math.pow((params.E * I) / ql, 1/3)
    result.F_sin = F_sin
    # 4.对应的电缆位置
    Z_sin: float = F_sin / ql
    result.Z_sin = Z_sin
    # 5.正弦长度
    spiral: SpiralResult = get_spiral_length(params)
    Z_hel = spiral.Z_hel
    L_sin = Z_hel - Z_sin
    result.L_sin = L_sin
    return result


# 确定正弦段区间
def get_sin_interval(params:FishParams) -> dict[str, float]:
    spiral: SpiralResult = get_spiral_length(params)
    Z_hel: float = spiral.Z_hel
    sin: SinResult = get_sin_length(params)
    Z_sin: float = sin.Z_sin
    sin_interval = {}
    sin_interval['low'] = Z_sin
    sin_interval['high'] = Z_hel
    return sin_interval


def get_sin_axial_deformation(params: FishParams) -> float:
    sin_length: SinResult = get_sin_length(params)
    li_value: float = sin_length.li
    R_value: float = (params.l - params.d) * 0.5
    x: float
    t: float
    R: float
    # 定义积分内容
    def integrand(x, R, li):
        factor_a = (2 * math.pi) / li - x
        return math.sqrt(1 + math.pow(factor_a * R * math.cos(factor_a * t), 2))

    # 定义目标函数
    def target_function(li_value, x, t, R_value):
        # 设置积分上下限
        a = 0
        b = li_value - x
        # 进行数值积分
        result, _ = quad(integrand, a, b, args=(R_value, li_value))

        return result

    # 使用二分法搜索使得积分结果近似为零的 li
    li_solution = bisect(target_function, 0, 10, args=(x, t, R_value))  # 设置搜索范围 [0, 10]

    print("找到的 li 解:", li_solution)

    return li_solution






# 确定直线段长度
def get_line_length(params:FishParams) -> float:
    sin: SinResult = get_sin_length(params)
    Z_sin = sin.Z_sin
    L_s = Z_sin
    return L_s


# 确定直线段区间
def get_line_interval(params:FishParams) -> dict[str, float]:
    sin: SinResult = get_sin_length(params)
    Z_sin: float = sin.Z_sin
    line_interval = {}
    line_interval['low'] = 0
    line_interval['high'] = Z_sin
    return line_interval


# 直线段变形
def get_line_trans(params:FishParams) -> float:
    A: float = math.pi * math.pow(params.d, 2) * 0.25
    L_s: float = get_line_length(params)
    ql: float = get_cable_weight(params)
    delta_ls: float = ql * math.pow(L_s, 2) * 0.5 / (params.E * A)
    return delta_ls



if __name__ == '__main__':
    params = FishParams()
    params.L_d = 2000
    params.L_t = 500
    params.rou_l = 1300
    params.q_s = 5.684
    params.d = 0.0124
    params.g = 9.8
    params.E = 51.8
    # result1 = get_drop_length(params)
    # print(result1)
    # result2 = get_cable_weight(params)
    # print(result2)
    # spiral_result = get_spiral_length(params)
    # print(JsonUtil.obj2str(spiral_result))
    sin_result = get_sin_length(params)
    print(JsonUtil.obj2str(sin_result))
